package com.upload;

import android.content.Context;
import android.graphics.Bitmap;

import com.idroid.async.AsyncWorker;
import com.squareup.okhttp.LongTimeAuzHttp;
import com.squareup.okhttp.Params;
import com.squareup.okhttp.RespondCallBack;

import java.io.ByteArrayOutputStream;
import java.io.Serializable;

/**
 * 图片上传,按照高宽来做压缩
 */
public class ImgUpload implements UploadUtil {
    private int targetWidth = 450, targetHeight = 450;

    private UpLoadListener mListener;
    private Context mContext;
    private String mPaths;
    private String mTag;
    private static final int MaxSize = 64;//单张图片允许最大64KB
    private String Url;
    private Photo photo;

    /**
     * 业务类型默认为other = 1
     */
    private int[] modules = {1, 1, 1, 1, 1};

    public ImgUpload(Context context,String url, String imgPaths, UpLoadListener l, String tag) {
        mContext = context.getApplicationContext();
        mPaths = imgPaths;
        mListener = l;
        Url=url;
        mTag = tag;
    }

    /**
     * 设置图片类型(目前最多支持5张图片),图片没有设置对应业务int的都取设置第一图片的int
     * @param businessModule
     */
    public UploadUtil setBusinessModule(int... businessModule) {
        if (businessModule == null)
            return this;
        int lenth = businessModule.length;
        for (int i = 0; i < modules.length; i++) {
            if (i < lenth)
                this.modules[i] = businessModule[i];
            else
                this.modules[i] = businessModule[0];
        }
        return this;
    }

    @Override
    public void doUpload() {

        AsyncWorker.execute(new Runnable() {
            @Override
            public void run() {
                doUpLoadInAsyncTask();
            }
        });

    }

    /**
     * 用异步线程操作图片的压缩与上传
     */
    private void doUpLoadInAsyncTask() {
            String photoPath = mPaths;
            byte[] photoBytes = ImageUtil.compressImage(
                    ImageUtil.decodeScaleImage(photoPath, targetWidth, targetHeight), MaxSize);
            if (null != photoBytes) {
                photo = new Photo(1, photoPath.substring(photoPath.lastIndexOf(".")).toLowerCase(), photoBytes,
                            Filetype.Img, 1);
            } else {
                if (mListener != null) {
                    mListener.onComplete(false, photoPath + "解码失败,请重新选择");
                    return;
                }
            }

        Params params = new Params();
        params.add("file", photo.data);
        LongTimeAuzHttp.Imgpost(Url, mPaths, new RespondCallBack() {
            @Override
            protected Object convert(String json) {
                return json;
            }

            @Override
            public void onSuccess(int code, Object data) {
                if (mListener != null) {
                    mListener.onComplete(true, (String)data);
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                if (mListener != null) {
                    mListener.onComplete(false, msg);
                }
            }
        },mTag);
    }
    /**
     * 表示图片的对象
     */
    class Photo implements Serializable {
        private int Flag;
        private String Extension;
        private byte[] data;
        private int fileType;
        private int businessModule;

        public Photo(int flag, String extension, byte[] data, int fileType, int module) {
            super();
            Flag = flag;
            Extension = extension;
            this.data = data;
            this.fileType = fileType;
            this.businessModule = module;
        }

        public int getFlag() {
            return Flag;
        }

        public void setFlag(int Flag) {
            this.Flag = Flag;
        }

        public String getExtension() {
            return Extension;
        }

        public void setExtension(String Extension) {
            this.Extension = Extension;
        }

        public byte[] getData() {
            return data;
        }

        public void setData(byte[] Data) {
            this.data = Data;
        }

        public int getFileType() {
            return fileType;
        }

        public void setFileType(int fileType) {
            this.fileType = fileType;
        }

        public int getBusinessModule() {
            return businessModule;
        }

        public void setBusinessModule(int businessModule) {
            this.businessModule = businessModule;
        }

    }

    /**
     * 图片转换为字节数组
     */
    private byte[] Bitmap2Bytes(Bitmap bm) {
        if (bm == null) {
            return null;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

}
